package com.zktr.dao;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


import com.zktr.pojo.Goods;


public class XlsDto2Excel {

	/**
	 * 
	 * @param xls
	 *            XlsDto实体类的一个对象
	 * @throws Exception
	 *             在导入Excel的过程中抛出异常
	 */
	public  String xlsDto2Excel(List<Goods> xls) throws Exception {
		// 获取总列数
		int CountColumnNum = xls.size();
		// 创建Excel文档
		HSSFWorkbook hwb = new HSSFWorkbook();
		
		Goods xlsDto = null;
		// sheet 对应一个工作页
		HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
		HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
		HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
		String[] names = new String[CountColumnNum];
		names[0] = "商品ID";
		names[1] = "类别ID";
		names[2] = "货单号";
		names[3] = "商品名称";
		names[4] = "品牌ID";
		names[5] = "商品数量";
		names[6] = "商品价格";
		names[7] = "报警数量";
		names[8] = "关键字";
		names[9]= "简单描述";
		names[10] = "详细描述";
		names[11] = "商图地址";
		names[12] = "获得积分";
		names[13]= "添加时间";
		names[14] = "是否删除";
		names[15] = "是否新品";
		names[16] = "是否热销";
		names[17] = "类型ID";
		names[18] = "等级晋升积分";
		for (int j = 0; j < CountColumnNum; j++) {
			firstcell[j] = firstrow.createCell(j);
			firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
		}
		for (int i = 0; i < xls.size(); i++) {
			// 创建一行
			HSSFRow row = sheet.createRow(i + 1);
			// 得到要插入的每一条记录
			xlsDto = xls.get(i);
			for (int colu = 0; colu <= 20; colu++) {
				// 在一行内循环
				HSSFCell one = row.createCell(0);
				one.setCellValue(xlsDto.getGoodsid());
				HSSFCell two = row.createCell(1);
				two.setCellValue(xlsDto.getCatid());
				HSSFCell three = row.createCell(2);
				three.setCellValue(xlsDto.getGoodssn());
				HSSFCell four = row.createCell(3);
				four.setCellValue(xlsDto.getGoodsname());
				HSSFCell cj = row.createCell(4);
				cj.setCellValue(xlsDto.getBrandid());
				HSSFCell fave = row.createCell(5);
				fave.setCellValue(xlsDto.getGoodsnumber());
				HSSFCell six = row.createCell(6);
				six.setCellValue(Double.parseDouble(xlsDto.getMarketprice()+""));
				HSSFCell seven = row.createCell(7);
				seven.setCellValue(xlsDto.getWarnnumber());
				HSSFCell eight = row.createCell(8);
				eight.setCellValue(xlsDto.getKeywords());
				HSSFCell ten = row.createCell(9);
				ten.setCellValue(xlsDto.getGoodsbrief());
				HSSFCell eleven  = row.createCell(10);
				eleven.setCellValue(xlsDto.getGoodsdesc());
				HSSFCell twelve = row.createCell(11);
				twelve.setCellValue(xlsDto.getGoodsimg());
				HSSFCell thirteen = row.createCell(12);
				thirteen.setCellValue(xlsDto.getIntegral());
				HSSFCell fourteen = row.createCell(13);
				fourteen.setCellValue(xlsDto.getAddtime());
				HSSFCell fifteen = row.createCell(14);
				fifteen.setCellValue(xlsDto.isIsdelete());
				HSSFCell sixteen = row.createCell(15);
				sixteen.setCellValue(xlsDto.isIsnew());
				HSSFCell seventeen = row.createCell(16);
				seventeen.setCellValue(xlsDto.isIshot());
				HSSFCell eighteen = row.createCell(17);
				eighteen.setCellValue(xlsDto.getTypeid());
				HSSFCell nineteen = row.createCell(18);
				nineteen.setCellValue(xlsDto.getGiveintegral());

			}
		}
		// 创建文件输出流，准备输出电子表格
		OutputStream out = new FileOutputStream("e:\\pldrxkxxmb.xls");
		hwb.write(out);
		out.close();
		String string="e:\\pldrxkxxmb.xls";
		return string;

	}

}
 
